package com.sky.controller.admin;

import com.sky.exception.CommonFileException;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import com.sky.utils.FileNameUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/admin/common")
@Api(tags = "图片上传相关接口")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;
    @ApiOperation("图片上传")
    @PostMapping("/upload")
    public Result puload(MultipartFile file) throws Exception {
        //将文件转换成UUID
        String fileObjecttName = FileNameUtils.packFileExt(file.getOriginalFilename());
        //使用工具类上传到阿里云OSS
        String filePath = aliOssUtil.upload(file.getBytes(), fileObjecttName);
        //判断文件是否为空,如果为空抛出自定义异常
        if (filePath == null){
            throw new CommonFileException("文件上传失败");
        }
        return Result.success(filePath);
    }

}
